<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script>

        function f1() {
            const p1 = new Promise((resolve, reject) => {
                const xhr = new XMLHttpRequest();
                xhr.open('get', 'http://43.138.81.225/php/1.php', true);
                xhr.send();
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === 4 && xhr.status === 200) {
                        let data = xhr.responseText;
                        resolve(data);
                    }
                }
            })
            return p1
        }

        function f2() {
            const p1 = new Promise((resolve, reject) => {
                const xhr = new XMLHttpRequest();
                xhr.open('get', 'http://43.138.81.225/php/2.php', true);
                xhr.send();
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === 4 && xhr.status === 200) {
                        let data = xhr.responseText;
                        resolve(data);
                    }
                }
            })
            return p1
        }

        function f3() {
            const p1 = new Promise((resolve, reject) => {
                const xhr = new XMLHttpRequest();
                xhr.open('get', 'http://43.138.81.225/php/3.php', true);
                xhr.send();
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === 4 && xhr.status === 200) {
                        let data = xhr.responseText;
                        resolve(data);
                    }
                }
            })
            return p1
        }

        // f1()
        // .then(data => {
        //     console.log(data);
        //     f2()
        //     .then(data => {
        //         console.log(data);
        //         f3()
        //         .then(data => {
        //             console.log(data);
        //         })
        //     })
        // })


        // 地狱回调的第一种解决方案   --- 链式调用  （不断的返回promise实例）

        let arr = [] ;

        f1()
            .then(data => {
                arr.push(data);
                return f2()
            })
            .then(data => {
                arr.push(data) ;
                return f3()
            })
            .then(data => {
                arr.push(data) ;
                console.log(arr);
            })



        // 问题：时间  p1 + p2 + p3


        // 地狱回调的第二种解决方案
        // Promise.all()    时间 = 花费时间最长的那一个

    </script>

</body>

</html>